import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/tools/log_utils.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';

import 'time_setting_state.dart';

class TimeSettingLogic extends BaseController {
  final TimeSettingState state = TimeSettingState();
  static const methodSetTimeSyncEnable = "setTimeSyncEnable";

  @override
  void onInit() {
    UTEPedDevice.instance.getUteConfig().syncData();
    super.onInit();
  }

  void setTimeSyncEnable(value) {
    state.isTimeSyncEnable.value = value;
    bRemoteReqAsync(methodSetTimeSyncEnable, {UTEPedConst.keyEnable: value})
        .then((success) => _onSetTimeSyncEnable(success, value));
  }

  void _onSetTimeSyncEnable(success, value) {
    if (success) {
      save(methodSetTimeSyncEnable, value);
    }
    else {
      state.isTimeSyncEnable.value = !value;
    }
  }

  void setCountry(String name, String code) {
    logger.d("set country $name-$code ");
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDeviceOptions;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    state.isTimeSyncEnable.value = read(methodSetTimeSyncEnable, defaultValue: false);
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }
}
